package com.cmict.data.system.controller.admin.encode;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.system.controller.admin.encode.vo.EncodeDetailsCreateReqVO;
import com.cmict.data.system.controller.admin.encode.vo.EncodeDetailsReqVO;
import com.cmict.data.system.dal.dataobject.encode.EncodeDetailsDO;
import com.cmict.data.system.service.encode.EncodeDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 编码详情")
@RestController
@RequestMapping("/system/encodedetails")
@Validated
public class EncodeDetailsController {

    @Resource
    private EncodeDetailsService encodeDetailsService;

    @PostMapping("/create")
    @ApiOperation("创建编码详情")
    @PreAuthorize("@ss.hasPermission('system:encodedetails:create')")
    public CommonResult<Long> createEncodeDetails(@Valid @RequestBody EncodeDetailsCreateReqVO reqVO) {
        Long encodeDetailsId = encodeDetailsService.createEncodeDetailsData(reqVO);
        return success(encodeDetailsId);
    }

    @PostMapping("/batchcreate")
    @ApiOperation("批量创建编码详情")
    @PreAuthorize("@ss.hasPermission('system:encodedetails:create')")
    public CommonResult<Boolean> batchCreateEncodeDetails(@Valid @RequestBody List<EncodeDetailsDO> list) {
        encodeDetailsService.createEncodeDetailsDataBatch(list);
        return success(true);
    }

    @PostMapping("/update")
    @ApiOperation("修改编码详情")
    @PreAuthorize("@ss.hasPermission('system:encodedetails:update')")
    public CommonResult<Boolean> updateEncodeDetails(@Valid @RequestBody EncodeDetailsCreateReqVO reqVO) {
        encodeDetailsService.updateEncodeDetailsData(reqVO);
        return success(true);
    }

    @PostMapping("/batchupdate")
    @ApiOperation("批量修改编码详情")
    @PreAuthorize("@ss.hasPermission('system:encodedetails:update')")
    public CommonResult<Boolean> updateBatchEncodeDetails(@Valid @RequestBody List<EncodeDetailsCreateReqVO> reqList) {
        reqList.forEach(encodeDetails->{
            encodeDetailsService.updateEncodeDetailsData(encodeDetails);
        });
        return success(true);
    }

    @GetMapping("/delete")
    @ApiOperation("删除编码详情")
    @ApiImplicitParam(name = "id", value = "主键", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:encodedetails:delete')")
    public CommonResult<Boolean> deleteEncodeDetails(@RequestParam("id") Long id) {
        encodeDetailsService.deleteEncodeDetailsData(id);
        return success(true);
    }

    @PostMapping("/batchdelete")
    @ApiOperation("批量删除编码详情")
    @PreAuthorize("@ss.hasPermission('system:encodedetails:delete')")
    public CommonResult<Boolean> batchdeleteEncodeDetails(@Valid @RequestBody List<Long> ids) {
        encodeDetailsService.batchdeleteEncodeDetails(ids);
        return success(true);
    }

    @ApiOperation("/获得编码详情列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:encodedetails:query')")
    public CommonResult<List<EncodeDetailsDO>> getDictEncodeDetailsList(@Valid EncodeDetailsReqVO reqVO) {
        return success(encodeDetailsService.getDictEncodeDetailsList(reqVO));
    }

    @ApiOperation("/查询编码详情详细")
    @ApiImplicitParam(name = "id", value = "主键", required = true, example = "1024", dataTypeClass = Long.class)
    @GetMapping(value = "/get")
    @PreAuthorize("@ss.hasPermission('system:encodedetails:query')")
    public CommonResult<EncodeDetailsDO> getEncodeDetails(@RequestParam("id") Long id) {
        return success(encodeDetailsService.getEncodeDetailsData(id));
    }
}
